//
//  AXFSuperMarketDetailController.swift
//  SpuerYangAngWang
//
//  Created by 杨茂盛 on 2016/12/23.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//

import UIKit
import SDWebImage
import SVProgressHUD
private let cellId = "cellId"
private let detailCellID = "detailCellID"
private let iconImageViewCellId = "iconImageViewCellId"
private let SuperMarketIntroduceCellId = "SuperMarketIntroduceCellId"
private let SuperMarketNotificationId = "SuperMarketNotificationId"
class AXFSuperMarketDetailController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    //详情页的数据
    var dataSource:ZHSellFastModel?
    
    //组尾高度
    var footViewHeight:CGFloat?
    //标题
    var  myTitle:String?
    override func viewDidLoad() {
        super.viewDidLoad()
     
        //设置导航栏
        self .setNav()
        //设置标题
        self.title = myTitle
        
        //搭建界面
        self.setUI()
    }
    
    //MARK: 搭建界面
    func setUI(){
     

        //创建 tableview
        let tableview = UITableView.init()
        self.view.addSubview(tableview)
        
        tableview.snp.makeConstraints { (make) in
           make.edges.equalTo(self.view)
        }
        
            //遵循代理
        tableview.dataSource = self
        tableview.delegate = self
        
        //注册商品图片 cell
        let productsNib = UINib.init(nibName: "SuperMarketImageViewCell", bundle: nil)
        tableview.register(productsNib, forCellReuseIdentifier: iconImageViewCellId)
        //注册 食品名称cell
        let DetailNib = UINib.init(nibName: "DetailCell", bundle: nil)
        tableview.register(DetailNib, forCellReuseIdentifier: detailCellID)

        //注册视频介绍 cell
        let SuperMarketIntroduceNib = UINib.init(nibName: "SuperMarketIntroduceCell", bundle: nil)
        tableview.register(SuperMarketIntroduceNib, forCellReuseIdentifier: SuperMarketIntroduceCellId)
        
        //提醒用户
        let SuperMarketNotificationNib = UINib.init(nibName: "SuperMarketNotification", bundle: nil)
        tableview.register(SuperMarketNotificationNib, forCellReuseIdentifier: SuperMarketNotificationId)
        //设置表尾
        var footView = UIImageView.init()
        
        footView = UIImageView.init(image: UIImage.init(named: "aaaa"))
        tableview.tableFooterView = footView
        
        //顶部和尾部不能再滚动
        tableview.bounces = false
        self.view.layoutIfNeeded()
        let shoppingView = AXFShoppingView.init(frame: CGRect(x: 0, y: ScreenHeight-60, width: ScreenWidth, height: 60))
        shoppingView.backgroundColor = #colorLiteral(red: 0.931610882, green: 0.931610882, blue: 0.931610882, alpha: 1)
        self.view.addSubview(shoppingView)
    
        //MARK: 购物车的点击事件
    
        shoppingView.clickPlusClosure = {
            print("加号按钮")
            if shoppingView.idex >= 5{
            self.alert()
            }

            
        }
        shoppingView.clickMinusClosure = {
               print("减号按钮")
        }
    }
    //MARK: 弹窗
    @objc private func alert() {
        
        let maskView = UIView(frame: UIScreen.main.bounds)
        maskView.backgroundColor = UIColor.init(white: 0, alpha: 0.5)
        
        //弹框
        let alertView = YMSAlertView.alertView()
        if self.dataSource == nil {
            return
        }
        let name:String = self.dataSource!.name!
        alertView.desc.text = name
        let keywindow = UIApplication.shared.keyWindow
        keywindow?.addSubview(maskView)
        keywindow?.addSubview(alertView)
        
        alertView.snp.makeConstraints { (make) in
            make.centerX.equalTo(keywindow!)
            make.centerY.equalTo(keywindow!).offset(-20)
            make.width.equalTo(310)
            make.height.equalTo(160)
        }
        
        alertView.alpha = 0
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 2, options: [], animations: {
            alertView.alpha = 1
            alertView.transform = CGAffineTransform.init(scaleX: 0.9, y: 0.9)
        }, completion: { isok in
            
            //延迟1s
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
                maskView.removeFromSuperview()
                alertView.removeFromSuperview()
            }
            
        })
    }
    
    //MARK: 设置导航栏
    func setNav(){
        //设置左右导航栏的按钮
        let itemLeft = UIBarButtonItem.init(title: "", imageName: "v2_goback", titleFontSize: 16, target: self, action: #selector(back))
        self.navigationItem.leftBarButtonItem = itemLeft
        let itemRight  = UIBarButtonItem.init(title: "分享", imageName: "", titleFontSize: 16, target: self, action: #selector(shared))
        self.navigationItem.rightBarButtonItem = itemRight
        //设置导航栏颜色
        self.navigationController?.navigationBar.barTintColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 0.3881635274)
    }
    //点击返回的方法
    func back(){
    self.navigationController!.popViewController(animated: true)
    }
    //MARK: 点击分享的方法
    func shared(){
        
        let titlearr = ["新浪微博","微信朋友圈","QQ 好友"]
        let imageArr = ["sinaweibo","wechat","tcentQQ"]
        let actionsheet = ActionSheetView.init(shareHeadOprationWith: titlearr, andImageArry: imageArr, andProTitle: "测试", and:ShowTypeIsShareStyle)

        actionsheet?.btnClick =  { (tag:Int) ->() in

            switch tag {
            case 0:
                print("新浪微博")
            case 1:
                print("微信朋友圈")
            case 2:
                print("QQ 好友")
            default:
                print("")
            }
        }
        UIApplication.shared.keyWindow?.addSubview(actionsheet!)
    }
    
    
  //MARK: -----------------------tableview 数据源方法------------------------------
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //第一行图片
        let model = self.dataSource
        if indexPath.row == 0 {
          let cell = tableView.dequeueReusableCell(withIdentifier: iconImageViewCellId, for: indexPath) as!SuperMarketImageViewCell
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0) //让 cell 的分隔线顶左
            cell.iconString = self.dataSource?.img
            return cell
        }
        
        if indexPath.row == 1 {
            let detailCell = tableView.dequeueReusableCell(withIdentifier: detailCellID, for: indexPath) as! DetailCell
            detailCell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0) //让 cell 的分隔线顶左
            detailCell.selectionStyle = .none//无选中
            //设置数据模型
            detailCell.model = model
            return detailCell
        }
        
        if indexPath.row == 5 {
            let notifiCell = tableView.dequeueReusableCell(withIdentifier: SuperMarketNotificationId, for: indexPath) as! SuperMarketNotification
//            notifiCell.textNameLabel.text = "商品签收后,如有任何问题,都是你的问题,别给我们打电话,谢谢"
            return notifiCell
        }
        
        let SuperMarketIntroduceCell = tableView.dequeueReusableCell(withIdentifier: SuperMarketIntroduceCellId, for: indexPath) as! SuperMarketIntroduceCell
        SuperMarketIntroduceCell.selectionStyle = .none
        SuperMarketIntroduceCell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        SuperMarketIntroduceCell.nameLabel.text = model?.brand_name
        SuperMarketIntroduceCell.backgroundColor = #colorLiteral(red: 0.9641894036, green: 0.9641894036, blue: 0.9641894036, alpha: 0.6877943065)
        
        if indexPath.row == 2 {
            SuperMarketIntroduceCell.titleLabel.text = "品牌"
            SuperMarketIntroduceCell.nameLabel.text = model?.brand_name

        }
        if indexPath.row == 3 {
             SuperMarketIntroduceCell.titleLabel.text = "产品规格"
            SuperMarketIntroduceCell.nameLabel.text = model?.specifics

        }
        if indexPath.row == 4 {
            SuperMarketIntroduceCell.titleLabel.text = "图文详情"
            SuperMarketIntroduceCell.nameLabel.text = ""
        }
        return SuperMarketIntroduceCell

    }
    
    //MARK: 返回行的高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 400
        }
        if indexPath.row == 1 {
            return 100
        }
        if indexPath.row == 5 {
            return 100
        }
        return 45
    }
    
    //MARK: 在当前界面消失的时候,再把导航栏颜色重新设置回去
    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.navigationBar.barTintColor = #colorLiteral(red: 0.9686274529, green: 0.8722725971, blue: 0.4904906162, alpha: 1)
    }
}
